package com.wzbc.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzbc.video.entity.File;
import com.wzbc.video.mapper.FileMapper;
import com.wzbc.video.service.FileService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements FileService {
    @Override
    public Integer insert(File file) {
        String suffix = file.getFileUrl().substring(file.getFileUrl().lastIndexOf(".") + 1);
//        if(validateChapter(suffix,file.getChapterId())){
//            return -1;
//        }
        return this.baseMapper.insert(file);
    }

    @Override
    public boolean validateChapter(String suffix,Long chapterId){
        LambdaQueryWrapper<File> wrapper = new LambdaQueryWrapper<File>()
                .eq(File::getSuffix,suffix)
                .eq(File::getChapterId,chapterId);
        if(this.baseMapper.selectList(wrapper)!=null){
            return false;
        }
        return true;
    }

    @Override
    public List<File> getFile(Long id) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<File>().eq(File::getChapterId,id));
    }

    @Override
    public Integer deleteById(Long id) {
        return this.baseMapper.deleteById(id);
    }

}
